package com.xinyiweidai.xinyiweidai.module.my;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.adapter.PayHistoryAdapter;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.PayListBean;
import com.xinyiweidai.xinyiweidai.util.Api;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Request;
/**
 * class:   PayHistoryActivity
 * data：   2018/9/28  18:14
 * author:  LanX
 * Description:  还款记录
 *
 */
@SuppressWarnings("ALL")
public class PayHistoryActivity extends BaseActivity {


    private TextView tv_hint;
    private RecyclerView recyclerView;
    private PayHistoryAdapter adapter;
    private List<PayListBean.DataBean> payList = new ArrayList();


    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay_history;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back, "我的订单", 0, true);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {

        tv_hint = (TextView) findViewById(R.id.tv_hint);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PayHistoryAdapter(payList,this);
        recyclerView.setAdapter(adapter);

        getData();
    }


    private void getData() {
        showLoading("正在加载");
        Map<String, String> map = new HashMap<>();
        map.put("uid", SpUtil.getInstance().getUserInfo().getUser_id());
        OkHttpUtils
                .get()
                .url(Api.loanHistory)
                .params(map)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        dismissLoading();
                        tv_hint.setClickable(true);
                        tv_hint.setText("加载异常,点击重载");
                        tv_hint.setOnClickListener(view -> getData());
                        MyToast.showError(PayHistoryActivity.this, "加载异常");
                    }

                    @Override
                    public void onResponse(String response) {
                        L.e(response);
                        dismissLoading();
                        parseData(response);
                    }
                });
    }


    private void parseData(String response) {
        PayListBean bean = new Gson().fromJson(response, PayListBean.class);
        if (bean.getCode() == 1) {
            payList.addAll(bean.getData());
            refreshView();
        }else{
            tv_hint.setVisibility(View.VISIBLE);
        }
    }


    private void refreshView() {
        tv_hint.setText("暂无订单");
        tv_hint.setClickable(false);
        tv_hint.setVisibility(payList.size() == 0 ? View.VISIBLE : View.GONE);
        adapter.notifyDataSetChanged();
    }

}
